<?php
/**
 * Introduction: 第三方登录授权
 * @author: 杨陈鹏
 * @date: 2016/4/05 16:04
 * @email: yangchenpeng@cdlinglu.com
 */

namespace Common\Logic;

use Common\Model;

class ThirdAuthLogic
{

    static public $model;

    public function __construct()
    {
        self::$model = new Model\ThirdAuthModel();
    }

    /**
     * @introduction: 插入数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     * @param array $data 需要插入的数据
     * @return array
     */
    static public function create($data)
    {
        if (!$data)
            return ['code' => 300, 'msg' => '参数错误'];

        if (self::$model->add($data))
            return ['code' => 200, 'msg' => '数据创建成功!'];

        return ['code' => 300, 'msg' => '数据创建失败!'];
    }

    /**
     * @introduction: 获取一条数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     * @param array or string $where 查询条件
     * @param $fileds 需要获取的字段
     * @return array
     */
    static public function getOneData($where = '', $fileds = '*')
    {

        return self::$model
            ->where($where)
            ->field($fileds)
            ->find();

    }

    /**
     * @introduction: 获取一条会员数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     * @param string $open_id 第三方open_id
     * @param string $source_type 第三方类型
     * @return array
     */
    static public function getOneMemberData($open_id, $source_type)
    {

        if (!$open_id || !$source_type)
            return ['code' => 300, 'msg' => '参数错误!'];

        $where = self::$model
            ->where(['open_id' => $open_id, 'source_type' => $source_type])
            ->field('uid')
            ->find();

        if ($where) {
            $memberLogic = D('Common/Member', 'Logic');

            $data = $memberLogic::getOneData($where);

            return ['code' => 200, 'msg' => '登录成功', 'data' => $data];

        }

        return ['code' => 300, 'msg' => '用户不存在'];


    }

    /**
     * @introduction: 绑定帐号
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     * @param string $data
     * @return array
     */
    static public function bind($data)
    {

        if (!$data['sign'] || !$data['open_id'] || !$data['source_type'])
            return ['code' => 300, 'msg' => '参数错误!'];

        $logic = D('Common/Member', 'Logic');

        $signType = $logic::getSignType($data['sign']);

        $userinfo = $logic::getOneData($signType);

        if (password($data['password'], $userinfo['encrypt']) != $userinfo['password'])
            return ['code' => 300, 'msg' => '密码错误!'];

        //检测当前账户是否已经绑定过当前第三方账户
        if(self::$model
            ->where(['uid' => $userinfo['uid'],'source_type' => $data['source_type']])
            ->count())
            return ['code' => 300, 'msg' => '当前账户已绑定过!'];


        $bind_data = [

            'open_id' => $data['open_id'],
            'source_type' => $data['source_type'],
            'uid' => $userinfo['uid'],
            'created' => NOW_TIME

        ];


        if (!self::$model->add($bind_data))
            return ['code' => 300, 'msg' => '绑定失败!'];

        return ['code' => 200, 'msg' => '绑定成功!', 'data' => $userinfo];

    }

}